/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.allotmanagementpc.impl;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.utility.date.DateUtil;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.common.pipeline.session.SessionInfo;
import com.lightkits.wms.database.dao.WmsExecuteLineMapper;
import com.lightkits.wms.database.dao.WmsRequestHeaderMapper;
import com.lightkits.wms.database.dao.WmsRequestLineMapper;
import com.lightkits.wms.database.model.allotmanagementpc.ExecuteLinePcModel;
import com.lightkits.wms.database.model.allotmanagementpc.RequestHeaderPcModel;
import com.lightkits.wms.database.model.allotmanagementpc.RequestLinePcModel;
import com.lightkits.wms.domain.allotmanagementpc.QueryAllotDocMesService;
import com.lightkits.wms.dto.allotmanagementpc.model.ExecuteLinePcDto;
import com.lightkits.wms.dto.allotmanagementpc.model.QueryParamsDto;
import com.lightkits.wms.dto.allotmanagementpc.model.RequestHeaderPcDto;
import com.lightkits.wms.dto.allotmanagementpc.model.RequestLinePcDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 魏静
 * @create 2019-07-17 16:
 * @desc 查询调拨单据信息服务
 **/
@Service
public class QueryAllotDocMesServiceImpl implements QueryAllotDocMesService {
    private final WmsRequestHeaderMapper headerMapper;

    private final WmsRequestLineMapper lineMapper;

    private final WmsExecuteLineMapper executeLineMapper;

    @Autowired
    public QueryAllotDocMesServiceImpl(WmsRequestHeaderMapper headerMapper,
                                       WmsRequestLineMapper lineMapper,
                                       WmsExecuteLineMapper executeLineMapper) {
        this.headerMapper = headerMapper;
        this.lineMapper = lineMapper;
        this.executeLineMapper = executeLineMapper;
    }

    /**
     * 获取调拨请求头
     *
     * @param dto 调拨头请求参数
     * @return List<RequestHeaderPcDto>
     * @throws Exception 时间异常
     */
    @Override
    public List<RequestHeaderPcDto> queryAllotHeaderMes(QueryParamsDto dto) throws Exception {
        List<RequestHeaderPcModel> pcDtos;
        if(!ObjectUtil.isEmpty(dto)){
           if (!ObjectUtil.isNullOrEmpty(dto.getBeginDate())
                   && DateUtil.isDate(dto.getBeginDate(), DateUtil.DEFAULT_DATE_PATTERN)) {
               throw new LightKitsBizException("the date is invalid");
           }
           if (!ObjectUtil.isNullOrEmpty(dto.getEndDate())
                   && DateUtil.isDate(dto.getEndDate(), DateUtil.DEFAULT_DATE_PATTERN)) {
               throw new LightKitsBizException("the date is invalid");
           }
           if(!ObjectUtil.isNullOrEmpty(dto.getEndDate())&&!ObjectUtil.isNullOrEmpty(dto.getBeginDate())){
               pcDtos = headerMapper.getRequestHeaderByAllot(dto.getRequestNumber(),
                       dto.getStatus(), dto.getDocumentType(),
                       DateUtil.toDate(dto.getBeginDate(), DateUtil.DEFAULT_DATE_PATTERN),
                       DateUtil.toDate(dto.getEndDate(), DateUtil.DEFAULT_DATE_PATTERN));
           }else{
               pcDtos = headerMapper.getRequestHeaderByAllot(dto.getRequestNumber(),
                       dto.getStatus(), dto.getDocumentType(), null,null);
           }

       }else{
           pcDtos=headerMapper.getRequestHeaderByAllot("","","",null,null);
       }
        if (ObjectUtil.isNullOrEmpty(pcDtos)) {
            return new ArrayList<>();
        }
        return convertHeadDto(pcDtos);
    }

    /**
     * 获取领料单行
     *
     * @param requestHeaderId 领料单头Id
     * @return RequestLinePcDto
     */
    @Override
    public List<RequestLinePcDto> queryAllotLineMes(String requestHeaderId) {
        List<RequestLinePcModel> pcDtos = lineMapper.getRequestLineByAllotPc(requestHeaderId);
        if (ObjectUtil.isNullOrEmpty(pcDtos)) {
            return new ArrayList<>();
        }
        return convertLineDto(pcDtos);
    }

    /**
     * 获取领料单执行表
     *
     * @param requestLineId 领料单行Id
     * @return ExecuteLinePcDto
     */
    @Override
    public List<ExecuteLinePcDto> queryExecutionLineMes(String requestLineId) {
        List<ExecuteLinePcModel> pcDtos = executeLineMapper.getExecutionLineByAllot(requestLineId,
                SessionInfo.languageCode());
        if (ObjectUtil.isNullOrEmpty(pcDtos)) {
            return new ArrayList<>();
        }
        return convertExeLineDto(pcDtos);
    }


    /**
     * 转换参数
     *
     * @param models 领料单参数
     * @return RequestHeaderPcDto
     */
    private List<RequestHeaderPcDto> convertHeadDto(List<RequestHeaderPcModel> models) {
        List<RequestHeaderPcDto> headerPcDtos = new ArrayList<>();
        models.forEach(target -> {
            RequestHeaderPcDto dto = new RequestHeaderPcDto();
            dto.setRequestHeaderId(target.getRequestHeaderId().toString());
            dto.setRequestNumber(target.getRequestNumber());
            dto.setDocumentType(target.getDocumentType());
            dto.setRequestStatus(target.getRequestStatus());
            dto.setFromEnterpriseId(target.getFromEnterpriseId().toString());
            dto.setFromEnterpriseName(target.getFromEnterpriseName());
            dto.setFromOrganizationId(target.getFromOrganizationId().toString());
            dto.setFromOrganizationName(target.getFromOrganizationName());
            dto.setToEnterpriseId(target.getToEnterpriseId().toString());
            dto.setToEnterpriseName(target.getToEnterpriseName());
            dto.setToOrganizationId(target.getToOrganizationId().toString());
            dto.setToOrganizationName(target.getToOrganizationName());
            dto.setRemark(target.getRemark());
            dto.setMakeDate(DateUtil.toDateString(target.getMakeDate()));
            headerPcDtos.add(dto);
        });
        return headerPcDtos;
    }

    /**
     * 转换参数
     *
     * @param models 领料单行参数
     * @return RequestLinePcDto
     */
    private List<RequestLinePcDto> convertLineDto(List<RequestLinePcModel> models) {
        List<RequestLinePcDto> linePcDtos = new ArrayList<>();
        models.forEach(target -> {
            RequestLinePcDto dto = new RequestLinePcDto();
            dto.setRequestHeaderId(target.getRequestHeaderId().toString());
            dto.setRequestLineId(target.getRequestLineId().toString());
            dto.setRequestLineNumber(target.getRequestLineNumber());
            dto.setItemId(target.getItemId().toString());
            dto.setItemDesc(target.getItemDesc());
            dto.setApplyQty(target.getApplyQty().toString());
            dto.setLotNumber(target.getLotNumber());
            dto.setUomCode(target.getUomCode());
            dto.setFromWarehouseId(target.getFromWarehouseId().toString());
            dto.setFromWarehouseName(target.getFromWarehouseName());
            dto.setToWarehouseId(target.getToWarehouseId().toString());
            dto.setToWarehouseName(target.getToWarehouseName());
            dto.setRemark(target.getRemark());
            dto.setReceiveQty(target.getReceiveQty().toString());
            dto.setSendQty(target.getSendQty().toString());

            dto.setTagControl(target.getTagControl());
            dto.setUnitPrice(target.getUnitPrice().toString());
            linePcDtos.add(dto);
        });
        return linePcDtos;
    }

    /**
     * 转换参数
     *
     * @param models 领料单执行表参数
     * @return ExecuteLinePcDto
     */
    private List<ExecuteLinePcDto> convertExeLineDto(List<ExecuteLinePcModel> models) {
        List<ExecuteLinePcDto> exeLinePcDtos = new ArrayList<>();
        models.forEach(target -> {
            ExecuteLinePcDto dto = new ExecuteLinePcDto();
            dto.setRequestHeaderId(target.getRequestHeaderId().toString());
            dto.setRequestLineId(target.getRequestLineId().toString());
            dto.setExecuteLineId(target.getExecuteLineId().toString());
            dto.setItemId(target.getItemId().toString());
            dto.setItemDesc(target.getItemDesc());
            dto.setOutOfLibQty(target.getOutOfLibQty().toString());
            dto.setLotNumber(target.getLotNumber());
            dto.setInOfLibQty(target.getInOfLibQty().toString());
            dto.setFromWarehouseId(target.getFromWarehouseId().toString());
            dto.setFromWarehouseName(target.getFromWarehouseName());
            dto.setToWarehouseId(target.getToWarehouseId().toString());
            dto.setToWarehouseName(target.getToWarehouseName());
            if (ObjectUtil.isEmpty(target.getOuterRequestWorkerId())) {
                dto.setOuterRequestWorkerId(target.getOuterRequestWorkerId().toString());
            }
            dto.setOuterRequestWorkerName(target.getOuterRequestWorkerName());
            if (ObjectUtil.isEmpty(target.getExecutedWorkerId())) {
                dto.setExecutedWorkerId(target.getExecutedWorkerId().toString());
            }
            dto.setExecutedWorkerName(target.getExecutedWorkerName());
            dto.setExecuteTime(DateUtil.toDateString(target.getExecuteTime()));
            exeLinePcDtos.add(dto);
        });
        return exeLinePcDtos;
    }
}
